package dog.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

import dog.model.Model;

public class LoginPanel extends JPanel{
	
	/**
	 * LOGIN PANEL
	 */

	private BufferedImage image;

	private static final long serialVersionUID = 461864343646134894L;
	JButton ok;
	JTextField username;
	JTextField password;
	Model model;
	
	LoginPanel(final Model model){
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		try
			{
			image=ImageIO.read(new File("LOGO.png"));
			}
		catch (IOException e)
			{
			image=null;
			System.err.println("Logo file does not exist!");
			}
		
		Dimension dimension = new Dimension(160,160);
		c.gridwidth=3;
		c.gridheight=1;
		c.gridx = 0;
		c.gridy = 0;
		add(new Box.Filler(dimension,dimension,dimension), c);
		
		c.gridwidth=1;
		c.gridheight=1;
		JLabel lab = new JLabel("Username: "); 
		c.gridx = 0;
		c.gridy = 1+0;
		add(lab, c);
		
		username = new JTextField(30);
		c.gridx = 1;
		add(username, c);
		
		lab = new JLabel("Password: "); 
		c.gridx = 0;
		c.gridy = 1+1;
		add(lab, c);
		
		password = new JPasswordField(30);
		password.addActionListener(new OkButtonListener());
		c.gridx = 1;
		add(password, c);
		
		ok = new JButton("OK");
		ok.addActionListener(new OkButtonListener());
		c.gridy = 1+2;
		c.gridx = 0;
		add(ok, c);
		
		this.model = model;
		
		c.gridwidth=3;
		c.gridy = 1+3;
		c.gridx = 0;
		dimension = new Dimension(8,16);
		add(new Box.Filler(dimension, dimension, dimension),c);
		
		c.gridwidth=1;
		c.gridy = 1+4;
		c.gridx = 0;
		JButton button;
		button = new JButton("Add/edit room");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
				{model.gui_setRoom(Integer.parseInt(JOptionPane.showInputDialog("id(-1 for new room): ","-1")),JOptionPane.showInputDialog("Room name: ","Awesome room"),Integer.parseInt(JOptionPane.showInputDialog("room size: ","2")));}});
		add(button,c);
		
		c.gridx = 1;
		button = new JButton("Add/edit department");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
				{model.gui_setDepartment(Integer.parseInt(JOptionPane.showInputDialog("id(-1 for new department): ","-1")),JOptionPane.showInputDialog("Department name: ","Awesome department"));}});
		add(button,c);
		
		c.gridx = 2;
		button = new JButton("Add/edit person");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
				{model.gui_setPerson(Integer.parseInt(JOptionPane.showInputDialog("id(-1 for new person): ","-1")),JOptionPane.showInputDialog("email: ","a@b.c"),JOptionPane.showInputDialog("Password: ","fu"),JOptionPane.showInputDialog("Real name: ","Knut"),Integer.parseInt(JOptionPane.showInputDialog("department","0")));}});
		add(button,c);

	}
	@Override
	public void paint(Graphics g)
		{
		super.paint(g);
		if (image!=null)
			g.drawImage(image, 0, 0, null);
		}
	
	public void loginFailed() {
		username.setEnabled(true);
		password.setEnabled(true);
		ok.setEnabled(true);
	}
		
	public class OkButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			{
			username.setEnabled(false);
			password.setEnabled(false);
			ok.setEnabled(false);
			model.gui_loginAttempt(username.getText(), password.getText());
			}
			
		}
		
	}
}
